package com.example.springbootfoodsafety.contrlloer;


import com.example.springbootfoodsafety.entity.User;
import com.example.springbootfoodsafety.po.Article;
import com.example.springbootfoodsafety.po.Speak;
import com.example.springbootfoodsafety.po.Tag;
import com.example.springbootfoodsafety.service.ManageService;
import com.example.springbootfoodsafety.util.*;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@RestController
public class ManageController {
    UuId u=new UuId();
    @Autowired
    private UploadUtil uploadUtil;
    @Autowired
    private ManageService service;
    MD5untils md5untils=new MD5untils();
    private String picturePrefix="http://localhost:8080/uploadFile/";

    /**
     * 注册
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    public Result<?> addUser(@RequestBody User user){
        System.out.println(user);
        System.out.println(service.forNameGetId(user.getUname()));
        if(!(service.forNameGetId(user.getUname())==0))
            return Result.error(new CodeMsg(404,"名字冲突"));

        if(user.getPassword()!=null)
        user.setPassword(md5untils.md5(user.getPassword()));
        service.addUser(user);
        return Result.success(user);
    }



    /***
     * 登录方法

     * @param session
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody User u, HttpSession session)
    {
        System.out.println("登录");
        int id=0;
        System.out.println(u);
        u.setPassword(md5untils.md5(u.getPassword()));
        User user =service.login(u.getUname(),u.getPassword());
       if(user!=null)
       {
           id=user.getId();
           if(user!=null)
           {
               session.setAttribute("user",user);
               user.setPassword("");
               user.setHeadImg(picturePrefix+user.getHeadImg());
               System.out.println(user);
               service.loginStatus(id);

               return Result.success(user);
           }
           else
           {
               return Result.error(new CodeMsg(404,"找不到"));
           }
       }
        return Result.error(new CodeMsg(404,"用户名或是密码错误"));
    }
    @PostMapping("/modifyStatic")
    public Result modifyStatic(Integer id){
        service.loginStatus(id);
        return Result.success("ok");
    }
    @GetMapping("/getAllUser")
    public Result getAllUser(){
        List<User> list =service.getAllUser();
        if(list!=null)
        {
            for(User u:list)
            {
                u.setHeadImg(picturePrefix+u.getHeadImg());
            }
            return Result.success(list);
        }
        return Result.error(new CodeMsg(400,"没有"));
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @PostMapping("/deleteUser")
    public boolean deleteUser(Integer id){
       boolean flag= service.deleteUser(id);
       return flag;
    }
    /***
     * 给list文章集合图片添加前缀
     * @param list
     */
    public void addPrefix(List<Article> list){
        for(Article a:list)
        {
            a.setAuthorheaderImg(picturePrefix+a.getAuthorheaderImg());
        }
    }
    private String modify(String luj){
        return "http://localhost:8080/"+luj;
    }
    /**
     *显示全部的文章
     * @return
     */
    @PostMapping("/getAllArticle")
    public Result getAllArticle(){
        System.out.println("进入获取全部文章");
        List<Article> list=service.getArticle();
        addPrefix(list);
        if(list!=null)
        return Result.success(list);
        else
            return Result.error(new CodeMsg(500,"未找到"));
    }

    @PostMapping("/getTagArticle")
    public Result getTagArticle(Integer tagID) {
        List<Article> list=service.getTagArticle(tagID);
        return Result.success(list);
    }
    @PostMapping("/updateTest")
    public void updateTest(@RequestBody Object user,MultipartFile uploadFile, HttpServletRequest request){

        System.out.println("访问文件参数上传接口");
        System.out.println(user);
        System.out.println(uploadFile.toString());
    }
    @GetMapping("/modifyPassword")
    public Result modifyPassword(Integer id, String password, String oldPassword) {
        System.out.println(id+password+oldPassword);
        System.out.println("id:"+id);
        System.out.println("password"+password);
        System.out.println("oldPassword"+oldPassword);
        if(service.findPassword(id,oldPassword)!=null)
        {
            boolean flag=service.modifyPassword(id,password);
            Result.success("ok");
        }
        else
            return Result.error(new CodeMsg(200,"password error"));
        return Result.error(new CodeMsg(200,"error"));
    }


    /***
     * 修改头像接口
     */
    @PostMapping("/modifyUserHeadImg.action")
    public Result modifyHeadImg( @RequestParam("file") MultipartFile file,Integer id){

        System.out.println(id);
        //1存图片
        String modify=UploadUtil.upload(file);
        System.out.println("modify:"+modify);
        service.modifyHeadImg(id,modify);
        return  Result.success(modify);

    }
    @GetMapping("/modifyhead.action")
    public boolean modifyhead(Integer id,String img){
        System.out.println("id:"+id+"img:"+img);
        return service.modifyHeadImg(id,img);
    }

    /**
     * 根据日期获取前6篇
     * @return
     */
    @PostMapping("/getLatestArticle")
    public Result  getLatestArticle(){
        System.out.println("根据日期获取前6篇");
        List<Article> list=service.getLatestArticle();
        if(list!=null)
        {
            addPrefix(list);
            return Result.success(list);
        }
        return Result.error(new CodeMsg(404,"未找到"));
    }
    /***
     * 根据点赞量获取前6篇文章
     * @return
     */
    @PostMapping("/getHeatRankingArticle")
    public Result getHeatRankingArticle(){
        System.out.println("根据点赞量获取前6篇文章");
        List<Article> list=service.getHeatRankingArticle();
        if(list!=null)
        {
            addPrefix(list);
            return Result.success(list);
        }
        return Result.error(new CodeMsg(404,"未找到"));
    }

    @PostMapping("/addArticle")
    public Result addArticle(@RequestBody Article article) {
        System.out.println(article);
        article.setTag(1);
        if(service.addArticle(article))
            return  Result.success("ok");
            return Result.error(new CodeMsg(200,"error"));
    }

    @PostMapping("/addArticleAndUser")
    public Result addArticleAndUser(
            @RequestParam("file") MultipartFile file,
            @RequestParam("summary") String summary,
            @RequestParam("author_id") Integer author_id,
            @RequestParam("body") String body,
            @RequestParam("tag") Integer tag,
            @RequestParam("title") String title

    ){

        String modify=UploadUtil.upload(file);
        modify="http://localhost:8080/uploadFile/"+modify;
        System.out.println("summary:"+summary+"\n author_id:"+author_id+"\nsummary:"+summary+"\ntag:"+tag+"\nbody:"+body+"  title"+title+"modify"+modify);
        Article article=new Article();
        article.setTag(tag);
        article.setAuthor_id(author_id);
        article.setImg(modify);
        article.setSummary(summary);
        article.setBody(body);
        article.setTitle(title);
        if(service.addArticle(article))
        return  Result.success("ok");
        return Result.error(new CodeMsg(200,"error"));

    }


    @PostMapping("/addSpeak")
    public Result addSpeak(@RequestBody Speak speak){
        System.out.println(speak);
        boolean flag=service.addSpeak(speak);
        return Result.success(flag);
    }



    /***
     *  根据对应的标签,获取到该标签下全部的文章
     * @param id
     * @return
     */
    @PostMapping("/showAtrcleWhereTag")
    public Result  showAtrcleWhereTag(Integer id){
        List<Article> list =service.showAtrcleWhereTag(id);
        if(list!=null)
        return Result.success(list);
        else
            return Result.error(new CodeMsg(200,"找不到"));
    }

    /****
     *
     * 获取全部评论
     * @param id
     * @return
     */
    @PostMapping("/getArticleSpeak")
    public Result getArticleSpeak(Integer id){
        System.out.println("wenzan"+id);
        List<Speak> list=service.getArticleSpeak(id);
        System.out.println(list);
        return Result.success(list);

    }
/*********************************类别******************************************/
    /***
     * 类别展示
     * @return
     */
    @PostMapping("/showAllTagAndCount")
    public Result showAllTagAndCount(){
        List<Tag> list=service.showAllTagAndCount();
        return Result.success(list);
    }

    @PostMapping("/getAllTag")
    public Result getAllTag(){
        List<Tag> list = service.getAlltag();
        return Result.success(list);
    }
    @PostMapping("/addTag")
    public  boolean addTag(String tagName){
        return service.addTag(tagName);
    }
    @PostMapping("/deleteTag")
    public Result deleteTag(int id){
        if(service.deleteTag(id))
            return Result.success("ok");
        else
            return Result.error(new CodeMsg(404,"error"));
    }




    @PostMapping("/getArticleNoStatic")
    public Result getArticleNoStatic() {
        List<Article> list=service.getArticleNoStatic();
        return Result.success(list);
    }

    @PostMapping("/passArticleStatic")
    public Result modifyArticleStatic(Integer id) {
        boolean flag=service.modifyArticleStatic(id,2);
        return Result.success(flag);
    }
    @PostMapping("/FailpassArticleStatic")
    public Result FailArticleStatic(Integer id) {
        boolean flag=service.modifyArticleStatic(id,0);
        return Result.success(flag);
    }

    @PostMapping("/deleteArtice")
    public Result deleteArtice(Integer id) {
        boolean flag=service.deleteArtice(id);
        return Result.success(flag);
    }


}
